<template>
  <div class="flexContent">
    <!-- 商品库存 -->
    <div class="searchBox">

      <el-form ref="form" :model="form" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="仓库名称" prop="warehouseId">
                  <el-select v-model="form.warehouseId" placeholder="请选择仓库名称" style="width: 95%">
                    <el-option v-for="warehouse in warehouseList" :key="warehouse.id" :label="warehouse.name"
                      :value="warehouse.id"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="商品名称" prop="name">
                  <el-input v-clearEmoji v-model.trim="form.name" clearable placeholder="请输入商品名称" style="width: 95%"
                    @keyup.enter.native="searchGoodsInvList"></el-input>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="商品分类" prop="categoryId">
                  <el-cascader ref="cascaderRef" clearable v-model="form.categoryId" filterable placeholder="请选择商品分类"
                    :options="categoryList" style="width: 95%"
                    :props="{ value: 'id', label: 'name', emitPath: false }"></el-cascader>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="商品类型" prop="type">
                  <el-select v-model="form.type" placeholder="请选择商品类型" clearable style="width: 95%">
                    <el-option label="整件商品" value="0"></el-option>
                    <el-option label="拆零商品" value="1"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="零库存" prop="showType">
                  <el-switch v-model="form.showType" active-color="#13ce66" inactive-color="#b6b6b6" style="width: 95%">
                  </el-switch>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchGoodsInvList" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetForm('form')" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" align="center" style="width: 100%"
          height="100%" size="medium">
          <vxe-column type="seq" title="序号" width="80" />
          <vxe-column field="firstCode" title="商品编码" :min-width="elTableAdaptationWidth('firstCode', tableData)" />
          <vxe-column field="name" title="商品名称" :min-width="elTableAdaptationWidth('name', tableData)" />
          <vxe-column field="mnemonic" title="助记符" :min-width="elTableAdaptationWidth('mnemonic', tableData)" />
          <vxe-column field="standard" title="规格" min-width="150" />
          <vxe-column field="unitName" title="单位" width="100" />
          <vxe-column field="producer" title="产地" :min-width="elTableAdaptationWidth('manufacturer', tableData)" />
          <vxe-column field="categoryName" title="商品分类" :min-width="elTableAdaptationWidth('categoryName', tableData)" />
          <vxe-column field="type" title="商品类型" min-width="120">
            <template slot-scope="{ row }">
              <div>
                {{ row.type == 0 ? "整件商品" : "拆零商品" }}
              </div>
            </template>
          </vxe-column>
          <vxe-column field="inventoryNum" title="库存数量" min-width="150" />
          <vxe-column field="outboundPrice" title="出仓价（元）" width="150"/>
          <vxe-column field="totalPrice" title="库存总价（元）" min-width="150" />
          <!-- <vxe-column  field="name" title="锁定数量"> -->
          <!-- </vxe-column> -->
          <vxe-column field="bigPacking" title="大包装" min-width="80" />
          <vxe-column field="productState" title="商品状态" width="100">
            <template slot-scope="{ row }">
              <div>
                <el-tag type="success" size="small" effect="plain" v-if="row.productState == 0">
                  正常
                </el-tag>
                <el-tag type="danger" size="small" effect="plain" v-else-if="row.productState == 1">
                  停用
                </el-tag>
                <el-tag type="info" size="small" effect="plain" v-else="row.productState == 2">
                  淘汰
                </el-tag>
              </div>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination background :current-page.sync="page.pageNum" :page-sizes="$pageSizes"
          :page-size.sync="page.pageSize" layout="total,-> , sizes, prev, pager, next, jumper" :total="page.total"
          @size-change="changePageSize" @current-change="getGoodsInvList" />
      </div>
    </div>
  </div>
</template>

<script>
import {
  reqGoodsInvList,
  reqWarehouseList,
  reqGoodsCategory,
} from "@/api/warehouse";
export default {
  name: "GoodsInventory",
  data() {
    return {
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      form: {
        name: "",
        warehouseId: "",
        categoryId: "",
        showType: false,
        type: "",
      },
      // 仓库列表
      warehouseList: [],
      // 分类列表
      categoryList: [],
      isSearch: false,
      tableData: [],
    };
  },
  mounted() {
    this.getWarehouseList();
    this.getCategoryList();
  },
  methods: {
    // 获取商品列表
    async getGoodsInvList() {
      let { pageNum, pageSize } = this.page;
      let { name, warehouseId, type, categoryId, showType } = this.form;
      if (!this.isSearch) {
        name = "";
        type = "";
        categoryId = "";
        if (this.warehouseList.length > 0) {
          warehouseId = this.warehouseList[0].id;
        }
        showType = undefined;
      }
      let reqObj = {
        pageNum,
        pageSize,
        name,
        warehouseId,
        type,
        categoryId,
        showType: showType ? 1 : undefined,
      };
      let res = await reqGoodsInvList(reqObj);
      if (res.code === 200) {
        this.tableData = res.data.list;
        this.page.total = res.data.total;
        this.form = { name, type, categoryId, warehouseId, showType };
        const $table = this.$refs.xTable1
        $table.loadData(this.tableData);
      }
    },
    // 修改每页几条
    changePageSize() {
      this.page.pageNum = 1;
      this.getGoodsInvList();
    },
    // 搜索
    searchGoodsInvList() {
      this.page.pageNum = 1;
      this.isSearch = true;
      this.getGoodsInvList();
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
      if (this.warehouseList.length > 0) {
        this.form.warehouseId = this.warehouseList[0].id;
      }
      this.page.pageNum = 1;
      this.page.pageSize = 20;
      this.isSearch = false;
      this.getGoodsInvList();
    },
    // 仓库列表
    async getWarehouseList() {
      let res = await reqWarehouseList();
      if (res.code === 200) {
        this.warehouseList = res.data;
        if (this.warehouseList.length > 0) {
          this.form.warehouseId = res.data[0].id;
        }
        this.getGoodsInvList();
      }
    },
    // 商品分类列表
    async getCategoryList() {
      let res = await reqGoodsCategory();
      if (res.code === 200) {
        this.categoryList = res.data;
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
